package com.hfdg.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.content.mapper.ContentMapper;
import com.hfdg.content.entity.Content;
import com.hfdg.content.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName ContentServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    ContentMapper contentMapper;

    @Override
    public String insert(Content content) {
        String uid = UUID.randomUUID().toString();
        content.setUid(uid);
        content.setDelFlag("0");
        content.setCreateTime(new Date());
        contentMapper.insert(content);
        return uid;
    }

    @Override
    public Integer update(Content content) {
        content.setUpdateTime(new Date());
        return contentMapper.update(content);
    }

    @Override
    public Integer  delete(String uid) {
        return contentMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return contentMapper.batchDelete(uids);
    }

    @Override
    public Content get(String uid) {
        return contentMapper.get(uid);
    }

    @Override
    public List<Content> list(Content content) {
    return  contentMapper.list(content);
    }

    @Override
    public PageInfo<Content> getPagingData(int offset, int limit,String orderBy, Content content) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<Content> pageInfo =new PageInfo<>(contentMapper.list(content));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(Content content){
        if(null!=content.getUid()){
            this.update(content);
        }else{
            this.insert(content);
        }
        return content.getUid();
    }
            }
